/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import sun.misc.Signal;
import sun.misc.SignalHandler;

/**
 *
 * @author Administrator
 */
public class OOII {
    public static void main(String[] args){
        
        ApplicationContext ctx=null;        
        if(args.length==0){
            ctx=new FileSystemXmlApplicationContext("OOII.xml");
        }
        else{
            ctx=new FileSystemXmlApplicationContext(args[0]);
        }
        final Controller controller = (Controller)ctx.getBean("controller");        
        
        /*Runtime.getRuntime().addShutdownHook(new Thread(){
            public void run(){
                System.out.println("用户选择退出");
                controller.stop();
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException ex) {
                    
                }
                System.out.println("正常退出");
            }        
        });*/
        
        SignalHandler handler = new SignalHandler(){
            public void handle(Signal sig){
                System.out.println("用户选择退出......");
                controller.stop();
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException ex) {
                    
                }
                System.out.println("正常退出");
            }            
        };
        
        Signal.handle(new Signal("INT"), handler);
        Signal.handle(new Signal("TERM"), handler);
        
        controller.run();
        System.out.println("bye-bye!");        
    }
}
